Detect the line endings convention of Cargo.lock (CRLF or LF) and preserve it
authorGleb Kozyrev <gleb@gkoz.com>
Fri, 30 Oct 2015 11:31:14 +0000 (13:31 +0200)
committerGleb Kozyrev <gleb@gkoz.com>
Fri, 30 Oct 2015 18:51:51 +0000 (20:51 +0200)
Fixes #2076

src/cargo/ops/lockfile.rs

index 3a0ef553f982205127829c7f695e990120485281..8f9489c14f252b6bb74345c018067afea9eb5363 100644 (file)
@@ -70,6 +70,9 @@ pub fn write_lockfile(dst: &Path, resolve: &Resolve) -> CargoResult<()> {
 
     // Load the original lockfile if it exists.
     if let Ok(orig) = paths::read(dst) {
+        if has_crlf_line_endings(&orig) {
+            out = out.replace("\n", "\r\n");
+        }
         if out == orig {
             // The lockfile contents haven't changed so don't rewrite it.
             // This is helpful on read-only filesystems.
@@ -81,6 +84,15 @@ pub fn write_lockfile(dst: &Path, resolve: &Resolve) -> CargoResult<()> {
     Ok(())
 }
 
+fn has_crlf_line_endings(s: &str) -> bool {
+    // Only check the first line.
+    if let Some(lf) = s.find('\n') {
+        s[..lf].ends_with('\r')
+    } else {
+        false
+    }
+}
+
 fn emit_package(dep: &toml::Table, out: &mut String) {
     out.push_str(&format!("name = {}\n", lookup(dep, "name")));
     out.push_str(&format!("version = {}\n", lookup(dep, "version")));